//
//  UITextField+Customs.m
//  aiyale-ios
//
//  Created by ChenWei on 16/5/16.
//  Copyright © 2016年 tongbinLi. All rights reserved.
//

#define BOTTOM_LINE_TAG 998

#define TEXTFIELD_LEFT_VIEW_TAG 1980
#define TEXTFIELD_LEFT_VIEW_TITLE_TAG 1981
#define TEXTFIELD_RIGHT_VIEW_TAG 1990
#define TEXTFIELD_RIGHT_VIEW_TITLE_TAG 1991

#import "UITextField+Customs.h"

@implementation UITextField (Customs)

- (void)addLeftSeparatorViewWithPadding:(CGFloat)padding {
    
    UIView *leftView = [[UIView alloc]init];
    leftView.frame = CGRectMake(0, 0, padding, padding);
    self.leftView = leftView;
    self.leftViewMode = UITextFieldViewModeAlways;
}
- (void)addRightSeparatorViewWithPadding:(CGFloat)padding {
    
    UIView *rightView = [[UIView alloc]init];
    rightView.frame = CGRectMake(0, 0, padding, padding);
    self.rightView = rightView;
    self.rightViewMode = UITextFieldViewModeAlways;
}

- (void)addLeftImageViewWithImage:(UIImage *)image size:(CGSize)size {
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, size.width, size.height);
    imageView.contentMode = UIViewContentModeCenter;
    self.leftView = imageView;
    self.leftViewMode = UITextFieldViewModeAlways;
}

- (void)addRightImageViewWithImage:(UIImage *)image size:(CGSize)size  {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, size.width, size.height);
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.rightView = imageView;
    self.rightViewMode = UITextFieldViewModeAlways;
}

- (void)addLeftTitle:(NSString *)title font:(UIFont *)font padding:(CGFloat)padding {
    UIView *leftView = [[UIView alloc]init];
        leftView.tag = TEXTFIELD_LEFT_VIEW_TAG;
        UILabel *titleLabel = [UILabel new];
        titleLabel.tag = TEXTFIELD_LEFT_VIEW_TITLE_TAG;
        titleLabel.font = font;
        titleLabel.text = title;
        [leftView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(leftView);
        }];
        
        CGSize labelSize = [titleLabel systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        leftView.frame = CGRectMake(0, 0, labelSize.width+2*padding, labelSize.height);
    self.leftView = leftView;
    self.leftViewMode = UITextFieldViewModeAlways;
}

- (void)addRightTitle:(NSString *)title font:(UIFont *)font padding:(CGFloat)padding {
    UIView *rightView = [[UIView alloc]init];
        rightView.tag = TEXTFIELD_RIGHT_VIEW_TAG;
        UILabel *titleLabel = [UILabel new];
        titleLabel.tag = TEXTFIELD_RIGHT_VIEW_TITLE_TAG;
        titleLabel.font = font;
        titleLabel.text = title;
        [rightView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(rightView);
        }];
        
        CGSize labelSize = [titleLabel systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        rightView.frame = CGRectMake(0, 0, labelSize.width+2*padding, labelSize.height);
        self.rightView = rightView;

    self.rightViewMode = UITextFieldViewModeAlways;
}

@end
